package com.solotiger.douban.ui.fragment.movie;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.solotiger.douban.R;
import com.solotiger.douban.biz.BaseSubscriber;
import com.solotiger.douban.biz.api.MovieApi;
import com.solotiger.douban.biz.model.movie.SubjectResult;
import com.solotiger.douban.ui.adapter.MovieAdapter;
import com.solotiger.douban.ui.adapter.MovieTopAdapter;
import com.solotiger.douban.ui.base.BaseBarFragment;
import com.solotiger.douban.ui.view.divider.Divider;
import com.solotiger.douban.util.DisplayUtil;
import com.solotiger.douban.util.RxHelper;
import com.solotiger.douban.util.ScreenUtil;

import butterknife.BindView;

/**
 * TOP250
 */
public class TopFragment extends BaseBarFragment {

    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private MovieTopAdapter mAdapter;

    /**
     * count 一页最大数据
     */
    private int start = 0, count = 20;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    /**
     * 初始化
     */
    private void init() {

        initRecyclerView();

        ProgressLayout dotView = new ProgressLayout(getContext());
        dotView.setColorSchemeResources(R.color.colorPrimary);
        mRefreshLayout.setHeaderView(dotView);
        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                loadData(true);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                loadData(false);
            }
        });

        mRefreshLayout.startRefresh();
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter = new MovieTopAdapter(getContext());
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    Fresco.getImagePipeline().resume();
                } else {
                    Fresco.getImagePipeline().pause();
                }
            }
        });

        mRefreshLayout.startRefresh();
//        loadData(true);
    }

    /**
     * 加载数据
     */
    private void loadData(boolean reload) {
        if (reload) {
            mAdapter.clear();
            start = 0;
        }
//        loading();
        MovieApi.getMovieTop250(start, count)
                .compose(RxHelper.bind(this))
                .subscribe(new BaseSubscriber<SubjectResult>() {
                    @Override
                    protected void _onError(Throwable e) {
                        loadFinished();
                        showMessage("失败!");
                    }

                    @Override
                    public void onNext(SubjectResult subjectResult) {
                        loadFinished();
                        if (subjectResult.getSubjects() != null) {
                            if (subjectResult.getSubjects().isEmpty()) {
                                showMessage("无更多数据");
                            } else {
                                mAdapter.notifyDataSetChanged(subjectResult.getSubjects());
                                start += mAdapter.getItemCount();
                            }
                        } else {
                            showMessage("无更多数据");
                        }
                    }
                });
    }

    private void loadFinished() {
//        closeLoading();
        mRefreshLayout.finishRefreshing();
        mRefreshLayout.finishLoadmore();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_theater;
    }

}
